package dao.impl;

import dao.AccountDao;
import entity.Account;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.JDBCUtils;

/**
 * @program: FinalExam
 * @description:
 * @create: 2021-12-14 17:12
 * @Date 2021/12/14
 **/

public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    public Account QueryMyBalance(String user_id) {
        Account account = null;
        try {
            String sql = "select * from account where user_id = ? ";
            account = template.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), user_id);
        } catch (DataAccessException e) {

        }
        return account;
    }

    public Boolean transferAccountsNyUsername(Double money, String user_id) {
        int update = 0;
        try {
            String sql = "update account set balance = ? where user_id = ? ";
            update = template.update(sql, money, user_id);
        } catch (DataAccessException e) {

        }
        return update > 0;
    }

    public Boolean withdrawalById(String id, Double money) {
        return null;
    }

    public Account findByAccountId(String accountId) {
        Account account = null;
        try {
            String sql = "select * from id = ? ";
            account = template.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class));
        } catch (DataAccessException e) {

        }
        return account;
    }

    public Boolean addAccount(String accountId, String gmt_create, String id) {
        Integer update = null;
        try {
            String sql = " insert into account (id,balance,gmt_create,gmt_modify,is_delete,user_id) values(?,?,?,?,?,?)";
            update = template.update(sql, accountId,0.0, gmt_create,"",0, id);
        } catch (DataAccessException e) {

        }
        return update > 0 ;
    }

    public Account QueryMyBalanceByUsername(String toUsername) {
        Account account = null;
        try {
            String sql = "select * from account where user_id = ? ";
            account = template.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), toUsername);
        } catch (DataAccessException e) {

        }

        return account;
    }

    public void saveMoney(Double money, String u_id) {
        String sql = "update account set balance = ?  where user_id = ? ";
        template.update(sql,money,u_id);
    }

    public void deleteAccount(String user_id, String gmt_modify) {
        String sql = "update account set gmt_modify = ? , is_delete = ? where user_id = ? ";
        template.update(sql,gmt_modify,1,user_id);
    }


}
